﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace DiverseStudents.UI
{
    internal static partial class UICreate
    {
        static GameObject uiRoot;
        static GameObject settingView;
        //  static GameObject mapSceenView;
        static GameObject detailIntentView;
        static GameObject detailZhuangchangView;
        static UICreate()
        {
            LoadViews();
        }

        public static void LoadViews()
        {
            try
            {
                uiRoot = Resources.Load<GameObject>("Prefabs/UI_Root");
                settingView = Resources.Load<GameObject>("Prefabs/UI/Main/SettingView@Main");
                // mapSceenView = Resources.Load<GameObject>("Prefabs/UI/Main/MapSceneView@Main");
                detailIntentView = Resources.Load<GameObject>("Prefabs/UI/Main/DetailIntentView@Main");
                detailZhuangchangView = Resources.Load<GameObject>("Prefabs/UI/Main/DetailZhuangchangView@Main");
            }
            catch (Exception e)
            {
                DLog.Error(e);
            }
        }

        public static void UnloadViews()
        {
            Resources.UnloadAsset(detailZhuangchangView);
            Resources.UnloadAsset(detailIntentView);
            Resources.UnloadAsset(settingView);
            Resources.UnloadAsset(uiRoot);
        }

        public static GameObject SubUIObject(string flag, GameObject parent,  params Type[] types)
        {
            GameObject gameObject = new GameObject($"Mod_{flag}", types);
            gameObject.transform.SetParent(parent.transform, false);
            return gameObject;
        }

        public static T CreatEmpty<T>(string flag = "") where T : Component
        {
            GameObject gameObject = new GameObject($"Mod_{flag}", typeof(T));
            var txt = gameObject.GetComponent<T>();
            return txt;
        }



        public static T CreatEmptyWithRender<T>(string flag = "") where T : Component
        {
            GameObject gameObject = new GameObject($"Mod_{flag}", typeof(CanvasRenderer), typeof(T));
            var txt = gameObject.GetComponent<T>();
            return txt;
        }

        private static T CreateErrorT<T>(string flag = "")
        {
            GameObject gameObject = new GameObject($"Mod_ErrorUI{flag}", typeof(T));
            var txt = gameObject.GetComponent<T>();
            return txt;
        }

        private static void ClearChilds(Transform @object)
        {
            var count = @object.childCount;
            for (var i = 0; i < count; i++)
            {
                var child = @object.GetChild(i);
                if (child != null)
                {
                    GameObject.Destroy(child.gameObject);
                }
            }
        }

    }
}
